﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.Linq;
using System.Text;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 表示 Marmat.Forms.Skin.ListBox 中项的集合。
    /// </summary>
    [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
    public class ObjectCollection : IList
    {

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly List<object> collection;
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly System.Windows.Forms.Control owner;

        /// <summary>
        /// 初始化 Marmat.Forms.Skin.ObjectCollection 的新实例。
        /// </summary>
        /// <param name="owner"></param>
        public ObjectCollection(System.Windows.Forms.Control owner)
        {
            this.owner = owner;
            this.collection = new List<object>();
        }

        /// <summary>
        /// 初始化 Marmat.Forms.Skin.ObjectCollection 的新实例。
        /// </summary>
        public ObjectCollection()
        {
        }

        /// <summary>
        /// 获取或设置指定索引处的元素。
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public object this[int index] { get => this.collection[index]; set => this.collection[index] = value; }



        /// <summary>
        /// 获取 System.Collections.Generic.List`1 中包含的元素数。
        /// </summary>
        public int Count => this.collection.Count;

        bool IList.IsReadOnly => ((IList)this.collection).IsReadOnly;

        bool IList.IsFixedSize => ((IList)this.collection).IsFixedSize;

        object ICollection.SyncRoot => ((ICollection)this.collection).SyncRoot;

        bool ICollection.IsSynchronized => ((ICollection)this.collection).IsSynchronized;


        /// <summary>
        /// 将对象添加到 System.Collections.Generic.List`1 的结尾处。
        /// </summary>
        /// <param name="item"></param>
        public int Add(object item)
        {
            this.collection.Add(item);
            this.owner?.Invalidate();
            return collection.Count;
        }

        /// <summary>
        /// 将指定集合的元素添加到 System.Collections.Generic.List`1 的末尾。
        /// </summary>
        /// <param name="collection"></param>
        public void AddRange(IEnumerable<object> collection)
        {
            this.collection.AddRange(collection);
            this.owner?.Invalidate();
        }

        /// <summary>
        /// 将指定集合的元素添加到 System.Collections.Generic.List`1 的末尾。
        /// </summary>
        /// <param name="collection"></param>
        public void AddRange(IEnumerable collection)
        {
            this.AddRange(collection.OfType<object>());
        }

        /// <summary>
        /// 从 System.Collections.Generic.List`1 中移除所有元素。
        /// </summary>
        public void Clear()
        {
            this.collection.Clear();
            this.owner?.Invalidate();
        }

        /// <summary>
        /// 确定某元素是否在 System.Collections.Generic.List`1 中。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(object item)
        {
            return this.collection.Contains(item);
        }

        /// <summary>
        /// 从目标数组的指定索引处开始，将整个 System.Collections.Generic.List`1 复制到兼容的一维数组。
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        public void CopyTo(Array array, int index)
        {
            ((ICollection)collection).CopyTo(array, index);
        }

        /// <summary>
        /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
        /// </summary>
        /// <returns></returns>
        public IEnumerator<object> GetEnumerator()
        {
            return this.collection.GetEnumerator();
        }

        /// <summary>
        /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        /// <summary>
        /// 搜索指定的对象，并返回整个 System.Collections.Generic.List`1 中第一个匹配项的从零开始的索引。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(object item)
        {
            return this.collection.IndexOf(item);
        }

        /// <summary>
        /// 将元素插入 System.Collections.Generic.List`1 的指定索引处。
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public void Insert(int index, object item)
        {
            this.collection.Insert(index, item);
            this.owner?.Invalidate();
        }

        /// <summary>
        /// 从 System.Collections.Generic.List`1 中移除特定对象的第一个匹配项。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public void Remove(object item)
        {
            this.collection.Remove(item);
            this.owner?.Invalidate();

        }

        /// <summary>
        /// 移除 System.Collections.Generic.List`1 的指定索引处的元素。
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            this.collection.RemoveAt(index);
            this.owner?.Invalidate();
        }

        /// <summary>
        /// 搜索与指定谓词所定义的条件相匹配的元素，并返回整个 System.Collections.Generic.List`1 中第一个匹配元素的从零开始的索引。
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        public int FindIndex(Predicate<object> match)
        {
            return this.collection.FindIndex(match);
        }

        public override string ToString()
        {
            return "(集合)";
        }


    }
}

